package com.nickstumpos.android.easyVolume.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;

import com.nickstumpos.android.easyVolume.R;
import com.nickstumpos.android.easyVolume.constants.EVCONSTANTS;
import com.nickstumpos.android.easyVolume.preset.presentation.PresetScreen;
import com.nickstumpos.android.easyVolume.streams.IStreamScreen;
import com.nickstumpos.android.easyVolume.streams.presentation.types.Screen;
import com.nickstumpos.android.easyVolume.volume.levels.VibrateLevel;
import com.nickstumpos.android.easyVolume.volume.levels.VolumeLevel;

public class ScreenManager {

	private Map<Screen, IStreamScreen>	screens;
	private Activity										main;
	private SharedPreferences						settings;
	private SharedPreferences.Editor		editor;
	private final static int						NUM_OF_SCREENS	= 6;
	PresetScreen												presets;

	public ScreenManager(Activity main) {
		this.screens = new HashMap<Screen, IStreamScreen>();
		this.main = main;
		this.settings = main.getSharedPreferences(EVCONSTANTS.PREFS_NAME, 0);
		this.editor = this.settings.edit();
		this.presets = (PresetScreen) main.findViewById(R.id.PresetsLayout);
		this.presets.assignPresetButtons(main);
	}

	public void clearReferences() {
		this.screens.clear();
		this.settings = null;
		this.editor = null;
		this.main = null;
	}

	public List<IStreamScreen> getSlaves() {
		List<IStreamScreen> ret = new ArrayList<IStreamScreen>();
		for (Screen screen : Screen.values()) {
			if (this.screens.get(screen).isSlave()) {
				ret.add(this.screens.get(screen));
			}
		}
		return ret;
	}

	private boolean isSetSlave(IStreamScreen screen) {
		return this.settings.getBoolean(screen.getName(), false);

	}

	public void onSlaveChange() {
		if (this.screens.get(Screen.MASTER) != null) {
			this.screens.get(Screen.MASTER).refresh();
		} else {
			for (Screen screen : Screen.values()) {
				this.screens.get(screen).refresh();
			}
		}
	}

	public void refreshAll() {
		for (Screen screen : Screen.values()) {
			if (!this.screens.get(screen).isSlave()) {
				this.screens.get(screen).refresh();
			}
		}
		if (this.screens.get(Screen.MASTER) != null) {
			this.screens.get(Screen.MASTER).refresh();
		}
	}

	public void registerScreen(IStreamScreen screen, Screen type) {
		screen.setManager(this);
		screen.assignButtons(this.main);
		this.screens.put(type, screen);
		if (!type.equals(Screen.MASTER)) {
			screen.setInitialLevel();
		}
		this.presets.registerScreen(screen, type);
	}

	public void restoreSettings() {
		if (this.settings.getAll().size() < ScreenManager.NUM_OF_SCREENS) {
			this.setDefaults();
		} 
			for (Screen screen : Screen.values()) {
				if (this.isSetSlave(this.screens.get(screen))) {
					this.screens.get(screen).setAsSlave();
				}
			}
		if (this.screens.get(Screen.MASTER) != null) {
			this.screens.get(Screen.MASTER).refresh();
		}
	}

	private void setDefaults() {
		this.editor.putBoolean(Screen.MEDIA.getName(), true);
		this.editor.putBoolean(Screen.RINGER.getName(), true);
		this.editor.putBoolean(Screen.NOTIFICATION.getName(), true);
		this.editor.putBoolean(Screen.SYSTEM.getName(), true);
		this.editor.putBoolean(Screen.VOICE.getName(), false);
		this.editor.putBoolean(Screen.ALARM.getName(), false);
		this.editor.commit();
	}

	public void setSlaveStatus(IStreamScreen screen, boolean slave) {
		this.editor.putBoolean(screen.getName(), slave);
		this.editor.commit();
	}

	public void setSlaveVibrate(VibrateLevel level) {
		for (Screen screen : Screen.values()) {
			if (this.screens.get(screen).isSlave()) {
				this.screens.get(screen).vibrateSwitch(level);
				;
			}
		}
	}

	public void setSlaveVolume(VolumeLevel level) {
		for (Screen screen : Screen.values()) {
			if (this.screens.get(screen).isSlave()) {
				this.screens.get(screen).setStreamVolumeLevel(level);
			}
		}
	}
}
